package view.macrogui
{
	import events.WorldEvent;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import model.macro.Unit;
	
	/**
	 */
	public class UnitList extends Sprite implements SidebarItem
	{
		private var items:Vector.<UnitItem>;
		private var background:Shape;
		
		public function UnitList()
		{
			items = new Vector.<UnitItem>();
			background = new Shape();
			addChild(background);
		}
		
		public function update(units:Vector.<Unit>):void
		{
			if (units == null)
			{
				units = new Vector.<Unit>();
			}
			
			while (units.length >= items.length)
			{
				var item:UnitItem = new UnitItem();
				addChild(item);
				items.push(item);
			}
			for (var i:int = 0; i < units.length; i++)
			{
				items[i].visible = true;
				items[i].update(units[i]);
				items[i].y = i * (UnitItem.HEIGHT + 2);
			}
			for (; i < items.length; i++)
			{
				items[i].visible = false;
			}
		}
		
		public function getHeight():int
		{
			if (visible)
			{
				return 150;
			}
			else
				return 0;
		}
		
		public function onUnitSelected(e:WorldEvent = null):void
		{
			var u:Unit = (e.data as Unit);
			for each (var item:UnitItem in items)
			{
				if (item.unit == u)
				{
					item.select();
				}
			}
		}
		
		public function onUnitDeselected(e:WorldEvent = null):void
		{
			var u:Unit = (e.data as Unit);
			for each (var item:UnitItem in items)
			{
				if (item.unit == u)
				{
					item.deselect();
				}
			}
		}
		
		public function onAllUnitsDeselected(e:WorldEvent = null):void
		{
			for each (var item:UnitItem in items)
			{
				item.deselect();
			}
		}
		
		private function getWidth():int
		{
			return SideBar.WIDTH - SideBar.PADDING * 2;
		}
	
	}

}